/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

// DlgScanJob.cpp : implementation file

#include "stdafx.h"
#include "CHScanner.h"
#include "DlgScanJob.h"
#include ".\dlgscanjob.h"

//---------------------------------------------------------------------------

IMPLEMENT_DYNAMIC(DlgScanJob, CRgnDialog)
DlgScanJob::DlgScanJob(CWnd* pParent /*=NULL*/)
	: CRgnDialog(DlgScanJob::IDD, pParent)
	, vPhase(0)
	, vMethod(0)
	, vData(_T(""))
{
	skinIn = NULL;
	retVal = false;

	buildButtonType = __bbtDisabled;
}

DlgScanJob::~DlgScanJob()
{
}

//---------------------------------------------------------------------------

void DlgScanJob::DoDataExchange(CDataExchange* pDX)
{
	CRgnDialog::DoDataExchange(pDX);

	DDX_Control(pDX, IDC_STA_PHASE, staPhase);
	DDX_Control(pDX, IDC_STA_METHOD, staMethod);
	DDX_Control(pDX, IDC_STA_DATA, staData);
	DDX_Control(pDX, IDC_STA_INFO, staInfo);
	DDX_Control(pDX, IDC_STA_LINE1, staLine1);
	DDX_Control(pDX, IDC_STA_LINE2, staLine2);
	DDX_Control(pDX, IDC_CMB_PHASE, cmbPhase);
	DDX_Control(pDX, IDC_CMB_METHOD, cmbMethod);
	DDX_Control(pDX, IDC_EDT_DATA, edtData);
	DDX_Control(pDX, IDC_BTN_OK, btnOk);
	DDX_Control(pDX, IDC_BTN_CANCEL, btnCancel);

	DDX_CBIndex(pDX, IDC_CMB_PHASE, vPhase);
	DDX_CBIndex(pDX, IDC_CMB_METHOD, vMethod);
	DDX_Text(pDX, IDC_EDT_DATA, vData);
	DDX_Control(pDX, IDC_BTN_BUILD, btnBuild);
}

//---------------------------------------------------------------------------

BEGIN_MESSAGE_MAP(DlgScanJob, CRgnDialog)
	ON_BN_CLICKED(IDC_BTN_OK, OnBnClickedBtnOk)
	ON_BN_CLICKED(IDC_BTN_CANCEL, OnBnClickedBtnCancel)
	ON_CBN_SELCHANGE(IDC_CMB_METHOD, &DlgScanJob::OnCbnSelchangeCmbMethod)
	ON_BN_CLICKED(IDC_BTN_BUILD, &DlgScanJob::OnBnClickedBtnBuild)
END_MESSAGE_MAP()

//---------------------------------------------------------------------------

BOOL DlgScanJob::OnInitDialog()
{
	CRgnDialog::OnInitDialog();

	SetProperties(skinIn, false, _T("Scan Job"));

	cmbPhase.AddString(wd.GetPhaseName(__wptNone));
	cmbPhase.AddString(wd.GetPhaseName(__wptDiscovery));
	cmbPhase.AddString(wd.GetPhaseName(__wptScan));

	cmbMethod.AddString(wd.GetMethodName(__wmtNone));
	cmbMethod.AddString(wd.GetMethodName(__wmtPing));
	cmbMethod.AddString(wd.GetMethodName(__wmtDns));
	cmbMethod.AddString(wd.GetMethodName(__wmtSyn));
	cmbMethod.AddString(wd.GetMethodName(__wmtFin));
	cmbMethod.AddString(wd.GetMethodName(__wmtNull));
	cmbMethod.AddString(wd.GetMethodName(__wmtXmas));
	cmbMethod.AddString(wd.GetMethodName(__wmtAck));
	cmbMethod.AddString(wd.GetMethodName(__wmtUdpSend));
	cmbMethod.AddString(wd.GetMethodName(__wmtIpProtocol));
	cmbMethod.AddString(wd.GetMethodName(__wmtNetBios));
	cmbMethod.AddString(wd.GetMethodName(__wmtWakeOnLAN));
	cmbMethod.AddString(wd.GetMethodName(__wmtWinSnmp));
	cmbMethod.AddString(wd.GetMethodName(__wmtDhcpDiscov));
	cmbMethod.AddString(wd.GetMethodName(__wmtPromisc));
	cmbMethod.AddString(wd.GetMethodName(__wmt4PingBcast));
	cmbMethod.AddString(wd.GetMethodName(__wmt6PingBcast));
	cmbMethod.AddString(wd.GetMethodName(__wmt6MLD));
	cmbMethod.AddString(wd.GetMethodName(__wmt6Ping));
	cmbMethod.AddString(wd.GetMethodName(__wmt6Syn));
	cmbMethod.AddString(wd.GetMethodName(__wmt6Fin));
	cmbMethod.AddString(wd.GetMethodName(__wmt6Null));
	cmbMethod.AddString(wd.GetMethodName(__wmt6Xmas));
	cmbMethod.AddString(wd.GetMethodName(__wmt6Ack));
	cmbMethod.AddString(wd.GetMethodName(__wmt6UdpSend));
	cmbMethod.AddString(wd.GetMethodName(__wmt6Proto));
	cmbMethod.AddString(wd.GetMethodName(__wmtWMI));
	cmbMethod.AddString(wd.GetMethodName(__wmtIgmp));
	cmbMethod.AddString(wd.GetMethodName(__wmtShutdown));

	if(skin!=NULL){
		staPhase.textColor  = skin->textColor;
		staMethod.textColor = skin->textColor;
		staData.textColor   = skin->textColor;
		staInfo.textColor   = skin->textColor;
	}

	WorkCfgToData();

	UpdateDataLabels(wce.methodType);

	UpdateData(FALSE);

	return TRUE;  // return TRUE unless you set the focus to a control
}

//---------------------------------------------------------------------------

void DlgScanJob::OnCancel()
{
	// CDialog::OnCancel();
}

void DlgScanJob::OnOK()
{
	// CDialog::OnOK();
}

//---------------------------------------------------------------------------

void DlgScanJob::Initialize(void)
{
	vPhase  = 0;
	vMethod = 0;
	vData.Empty();

	buildButtonType = __bbtDisabled;
}

void DlgScanJob::PhaseToData(void)
{
	switch(wce.phaseType){
		case __wptDiscovery :
			vPhase = 1;
			break;
		case __wptScan :
			vPhase = 2;
			break;
		default :
			vPhase = 0;
			break;
	}
}

void DlgScanJob::PhaseFromData(void)
{
	switch(vPhase){
		case 1 :
			wce.phaseType = __wptDiscovery;
			break;
		case 2 :
			wce.phaseType = __wptScan;
			break;
		default :
			wce.phaseType = __wptNone;
			break;
	}
}

void DlgScanJob::MethodToData(void)
{
	switch(wce.methodType){
		case __wmtPing :
			vMethod = 1;
			break;
		case __wmtDns :
			vMethod = 2;
			break;
		case __wmtSyn :
			vMethod = 3;
			break;
		case __wmtFin :
			vMethod = 4;
			break;
		case __wmtNull :
			vMethod = 5;
			break;
		case __wmtXmas :
			vMethod = 6;
			break;
		case __wmtAck :
			vMethod = 7;
			break;
		case __wmtUdpSend :
			vMethod = 8;
			break;
		case __wmtIpProtocol :
			vMethod = 9;
			break;
		case __wmtNetBios :
			vMethod = 10;
			break;
		case __wmtWakeOnLAN :
			vMethod = 11;
			break;
		case __wmtWinSnmp :
			vMethod = 12;
			break;
		case __wmtDhcpDiscov :
			vMethod = 13;
			break;
		case __wmtPromisc :
			vMethod = 14;
			break;
		case __wmt4PingBcast :
			vMethod = 15;
			break;
		case __wmt6PingBcast :
			vMethod = 16;
			break;
		case __wmt6MLD :
			vMethod = 17;
			break;
		case __wmt6Ping :
			vMethod = 18;
			break;
		case __wmt6Syn :
			vMethod = 19;
			break;
		case __wmt6Fin :
			vMethod = 20;
			break;
		case __wmt6Null :
			vMethod = 21;
			break;
		case __wmt6Xmas :
			vMethod = 22;
			break;
		case __wmt6Ack :
			vMethod = 23;
			break;
		case __wmt6UdpSend :
			vMethod = 24;
			break;
		case __wmt6Proto :
			vMethod = 25;
			break;
		case __wmtWMI :
			vMethod = 26;
			break;
		case __wmtIgmp :
			vMethod = 27;
			break;
		case __wmtShutdown :
			vMethod = 28;
			break;

		default:
			vMethod = 0;
			break;
	}
}

void DlgScanJob::MethodFromData(void)
{
	switch(vMethod){
		case 1 :
			wce.methodType = __wmtPing;
			break;
		case 2 :
			wce.methodType = __wmtDns;
			break;
		case 3 :
			wce.methodType = __wmtSyn;
			break;
		case 4 :
			wce.methodType = __wmtFin;
			break;
		case 5 :
			wce.methodType = __wmtNull;
			break;
		case 6 :
			wce.methodType = __wmtXmas;
			break;
		case 7 :
			wce.methodType = __wmtAck;
			break;
		case 8 :
			wce.methodType = __wmtUdpSend;
			break;
		case 9 :
			wce.methodType = __wmtIpProtocol;
			break;
		case 10 :
			wce.methodType = __wmtNetBios;
			break;
		case 11 :
			wce.methodType = __wmtWakeOnLAN;
			break;
		case 12 :
			wce.methodType = __wmtWinSnmp;
			break;
		case 13 :
			wce.methodType = __wmtDhcpDiscov;
			break;
		case 14 :
			wce.methodType = __wmtPromisc;
			break;
		case 15 :
			wce.methodType = __wmt4PingBcast;
			break;
		case 16 :
			wce.methodType = __wmt6PingBcast;
			break;
		case 17 :
			wce.methodType = __wmt6MLD;
			break;
		case 18 :
			wce.methodType = __wmt6Ping;
			break;
		case 19 :
			wce.methodType = __wmt6Syn;
			break;
		case 20 :
			wce.methodType = __wmt6Fin;
			break;
		case 21 :
			wce.methodType = __wmt6Null;
			break;
		case 22 :
			wce.methodType = __wmt6Xmas;
			break;
		case 23 :
			wce.methodType = __wmt6Ack;
			break;
		case 24 :
			wce.methodType = __wmt6UdpSend;
			break;
		case 25 :
			wce.methodType = __wmt6Proto;
			break;
		case 26 :
			wce.methodType = __wmtWMI;
			break;
		case 27 :
			wce.methodType = __wmtIgmp;
			break;
		case 28 :
			wce.methodType = __wmtShutdown;
			break;

		default:
			wce.methodType = __wmtNone;
			break;
	}
}

void DlgScanJob::WorkCfgToData(void)
{
	PhaseToData();
	MethodToData();

	vData = wce.data;
}

void DlgScanJob::WorkCfgFromData(void)
{
	PhaseFromData();
	MethodFromData();

	wce.data = vData.Trim();
}

//---------------------------------------------------------------------------

void DlgScanJob::ResizeChilds(void)
{
	int bw, bw2, bh;		///< size of button controls
	int lw, lh;		///< size of label controls
	int ew, eh;		///< size of edit and combobox controls
	int ih;			///< height of the 'info' label
	int ml;			///< for vertical-center-aligning the labels with edit and combo controls
	int tw;			///< total required width
	int cw, ch;		///< client region's width and height
	int mx, my;		///< left and top space for displaying the objects in the center of the window
	int dd;			///< space between objects
	RECT rect;
	int posx1, posx2, posy;

	dd = 2;

	btnOk.GetWindowRect(&rect);
	bw = rect.right - rect.left;
	bh = rect.bottom - rect.top;

	btnBuild.GetWindowRect(&rect);
	bw2 = rect.right - rect.left;

	staPhase.GetWindowRect(&rect);
	lw = rect.right - rect.left;

	cmbPhase.GetWindowRect(&rect);
	ew = rect.right - rect.left;
	eh = rect.bottom - rect.top;

	lh = eh;

	ih = 146;

	///< tw is computed according to the layout
	tw = lw + dd + ew;

	cw = clientRect.right - clientRect.left;
	ch = clientRect.bottom - clientRect.top;

	mx = dd;
	my = dd;

	posx1 = clientRect.left + mx;
	posx2 = posx1 + dd + lw;
	posy = clientRect.top + my;
	ml    = dd + eh;		// ml is reused here

	staPhase.MoveWindow(posx1, posy, lw, lh, TRUE);
	cmbPhase.MoveWindow(posx2, posy, ew, eh, TRUE);
	posy += ml;
	staMethod.MoveWindow(posx1, posy, lw, lh, TRUE);
	cmbMethod.MoveWindow(posx2, posy, ew, eh, TRUE);
	posy += ml;
	staData.MoveWindow(posx1, posy, lw, lh, TRUE);
	edtData.MoveWindow(posx2, posy, ew - dd - bw2, eh, TRUE);
	btnBuild.MoveWindow(posx2 + ew - bw2, posy, bw2, eh, TRUE);
	posy += ml;

	staLine1.MoveWindow(posx1, posy, tw, lh, TRUE);
	posy += dd + lh;

	staInfo.MoveWindow(posx1, posy, tw, ih, TRUE);
	posy += dd + ih;

	staLine2.MoveWindow(posx1, posy, tw, lh, TRUE);
	posy += dd + lh;

	posx1 = cw - bw;
	posx2 = posx1 - dd - bw;
	btnCancel.MoveWindow(posx1, posy, bw, bh, TRUE);
	btnOk.MoveWindow(posx2, posy, bw, bh, TRUE);
}

bool DlgScanJob::GetMinimumSize(RECT* rectIn)
{
	if(rectIn==NULL) return false;

	rectIn->left   = 0;
	rectIn->right  = 319;
	rectIn->top    = 0;
	rectIn->bottom = 204;

	return true;
}

//---------------------------------------------------------------------------

void DlgScanJob::OnBnClickedBtnOk()
{
	UpdateData(TRUE);
	WorkCfgFromData();

	retVal = true;
	EndDialog(IDOK);
}

void DlgScanJob::OnBnClickedBtnCancel()
{
	retVal = false;
	EndDialog(IDABORT);
}

//---------------------------------------------------------------------------

void DlgScanJob::UpdateDataLabels(WorkMethodType wmtIn)
{
	CString str;

	str = wd.GetMethodParamName(wmtIn);
	staData.SetWindowText(str);

	if(str.IsEmpty()){
		edtData.EnableWindow(FALSE);
	}
	else{
		edtData.EnableWindow(TRUE);
	}

	str = wd.GetMethodDescription(wmtIn);
	staInfo.SetWindowText(str);

	buildButtonType = wd.GetBuildButtonType(wmtIn);
	if(buildButtonType==__bbtDisabled)
		btnBuild.EnableWindow(FALSE);
	else
		btnBuild.EnableWindow(TRUE);

	Invalidate();
	UpdateWindow();
}

//---------------------------------------------------------------------------

void DlgScanJob::OnCbnSelchangeCmbMethod()
{
	UpdateData(TRUE);
	WorkCfgFromData();

	UpdateDataLabels(wce.methodType);
}

//---------------------------------------------------------------------------

void DlgScanJob::OnBnClickedBtnBuild()
{
	UpdateData(TRUE);
	wce.data = vData;

	switch(buildButtonType){
		case __bbtWMI :
			dlgWmiSel.skinIn = skinIn;
			dlgWmiSel.retSelection = _tstoi(wce.data);
			dlgWmiSel.SetHelpInfo(helpInfo, HELPID_DLG_WMISEL);
			dlgWmiSel.DoModal();
			if(dlgWmiSel.retVal){
				wce.data.Format(_T("%d"), dlgWmiSel.retSelection);
				vData = wce.data;
				UpdateData(FALSE);
			}
			break;

		case __bbtWOL :
			dlgBldWOL.skinIn = skinIn;
			dlgBldWOL.dlgResult = wce.data;
			dlgBldWOL.SetHelpInfo(helpInfo, HELPID_DLG_BLDWOL);
			dlgBldWOL.DoModal();
			if(dlgBldWOL.retVal){
				wce.data = dlgBldWOL.dlgResult;
				vData = wce.data;
				UpdateData(FALSE);
			}
			break;

		case __bbtShutdown :
			dlgBldShutdown.skinIn = skinIn;
			dlgBldShutdown.dlgResult = wce.data;
			dlgBldShutdown.SetHelpInfo(helpInfo, HELPID_DLG_BLDSHUTDOWN);
			dlgBldShutdown.DoModal();
			if(dlgBldShutdown.retVal){
				wce.data = dlgBldShutdown.dlgResult;
				vData = wce.data;
				UpdateData(FALSE);
			}
			break;
	}
}

//---------------------------------------------------------------------------
